package com.xuef.service.impl;

import com.xuef.entity.Role;
import com.xuef.entity.User;
import com.xuef.service.IRoleService;
import com.xuef.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by moveb on 2018/5/20.
 * 处理用户登录认证 服务
 */
@Service
public class AuthServiceImpl implements UserDetailsService {
    private static final Logger logger = LoggerFactory.getLogger(AuthServiceImpl.class);

    @Autowired
    private IUserService userServiceImpl;

    @Autowired
    private IRoleService roleServiceImpl;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userServiceImpl.findByUsername(username);
        if(user != null) {
            List<Role> roleList = roleServiceImpl.findByUserId(user.getId());
            user.setAuthorities(roleList);
        }

        logger.info(user.toString());
        return user;
    }
}
